package com.cdm.persistence;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 * CdmLicenseTemplate entity.
 * 
 * @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "cdm_license_template", catalog = "cdm", uniqueConstraints = {})
public class LicenseTemplate implements java.io.Serializable {

	// Fields

	private Long licenseTemplateId;
	private String description;
	private Date createTime;
	private Long version;

	private Collection<LicenseAsset> assets;

	private Collection<LicensePermission> permissions;

	public static void main(String[] args) {
		LicenseTemplate t = new LicenseTemplate(003l, "des", null, 1l);
		List<LicensePermission> pers = new ArrayList<LicensePermission>();
		pers.add(new LicensePermission(5l, 1l, 255l, null, null, 999l, "rrr", "rrrr", 99l));
		t.setPermissions(pers);
		EntityManagerHelper.beginTransaction();
		EntityManagerHelper.getEntityManager().persist(t);
		EntityManagerHelper.commit();

		LicenseTemplate find = EntityManagerHelper.getEntityManager().find(LicenseTemplate.class, 3l);
		System.out.println(find.getDescription());
		System.out.println(find.getPermissions().size());
		System.out.println(find.getDescription());
		System.out.println(find.getDescription());
	}

	// Constructors
	@ManyToMany(cascade = CascadeType.PERSIST)
	@JoinTable(catalog = "cdm", name = "cdm_license_asset_use", joinColumns = { @JoinColumn(name = "cdm_license_template_id", referencedColumnName = "license_template_id") }, inverseJoinColumns = @JoinColumn(name = "cdm_license_asset_id", referencedColumnName = "asset_id"))
	public Collection<LicenseAsset> getAssets() {
		return assets;
	}

	public void setAssets(Collection<LicenseAsset> assets) {
		this.assets = assets;
	}

	@ManyToMany(cascade = CascadeType.PERSIST, targetEntity = LicensePermission.class)
	@JoinTable(catalog = "cdm", name = "cdm_license_permission_use", joinColumns = { @JoinColumn(name = "cdm_license_template_id", referencedColumnName = "license_template_id") }, inverseJoinColumns = @JoinColumn(name = "cdm_license_permission_id", referencedColumnName = "permission_id"))
	public Collection<LicensePermission> getPermissions() {
		return permissions;
	}

	public void setPermissions(Collection<LicensePermission> permissions) {
		this.permissions = permissions;
	}

	/** default constructor */
	public LicenseTemplate() {
	}

	/** full constructor */
	public LicenseTemplate(Long licenseTemplateId, String description, Date createTime, Long version) {
		this.licenseTemplateId = licenseTemplateId;
		this.description = description;
		this.createTime = createTime;
		this.version = version;
	}

	// Property accessors
	@Id
	@Column(name = "license_template_id", unique = true, nullable = false, insertable = true, updatable = true)
	public Long getLicenseTemplateId() {
		return this.licenseTemplateId;
	}

	public void setLicenseTemplateId(Long licenseTemplateId) {
		this.licenseTemplateId = licenseTemplateId;
	}

	@Column(name = "description", length = 2000)
	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "create_time", length = 19)
	public Date getCreateTime() {
		return this.createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "version", unique = false, nullable = false, insertable = true, updatable = true)
	public Long getVersion() {
		return this.version;
	}

	public void setVersion(Long version) {
		this.version = version;
	}

}